package cn.zhfeat.hatch.web.service.impl.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.zhfeat.core.base.BaseResultModel;
import cn.zhfeat.core.base.ResultStatus;
import cn.zhfeat.core.shiro.ShiroSessionData;
import cn.zhfeat.hatch.dto.company.CompanyDepartmentDto;
import cn.zhfeat.hatch.dto.store.BaseStoreInfoDto;
import cn.zhfeat.hatch.dto.store.BaseStoreToUserDto;
import cn.zhfeat.hatch.dto.sysuser.SysRoleDto;
import cn.zhfeat.hatch.dto.sysuser.SysUserDto;
import cn.zhfeat.hatch.dto.sysuser.SysUserRoleDto;
import cn.zhfeat.hatch.entity.company.CompanyDepartment;
import cn.zhfeat.hatch.entity.store.BaseStoreInfo;
import cn.zhfeat.hatch.entity.store.BaseStoreToUser;
import cn.zhfeat.hatch.entity.sysuser.SysRole;
import cn.zhfeat.hatch.entity.sysuser.SysUser;
import cn.zhfeat.hatch.entity.sysuser.SysUserRole;
import cn.zhfeat.hatch.rpc.IAccountRpcService;
import cn.zhfeat.hatch.rpc.IBaseStoreInfoRpcService;
import cn.zhfeat.hatch.rpc.IDepartmentRpcService;
import cn.zhfeat.hatch.rpc.IRoleRpcService;
import cn.zhfeat.hatch.web.service.IPersonalCenterService;
import cn.zhfeat.hatch.web.utils.ConfigUtil;
import cn.zhfeat.hatch.web.utils.ShiroSessionDataUtil;
import cn.zhfeat.utils.MD5Util;
import cn.zhfeat.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;


/**   
 * @Description:  设置 - 个人中心 
 * 
 * @author: ligang
 * @date:   2018年5月18日 下午4:02:45    
 * @version 1.0.0.1 
 */
@Service("personalCenterService")
public class PersonalCenterServiceImpl implements IPersonalCenterService {

	@Autowired
	private IAccountRpcService accountRpcService;
	
	@Autowired
	private IRoleRpcService roleRpcService;
	
	@Autowired
	private IBaseStoreInfoRpcService baseStoreInfoRpcService;
	
	@Autowired
	private IDepartmentRpcService departmentRpcService;
	
	/**
	 * 
	 * @Description: 设置 - 个人中心 - 用户信息
	 * 
	 * @author: ligang
	 * @date:   2018年5月18日 下午4:24:06    
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxUserInfo() {
		
		BaseResultModel model = new BaseResultModel();
		//获取用户ID 
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		Long id = shiroSessionData.getUserId();
		if(id == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		//查询用户信息
		SysUserDto dto = new SysUserDto();
		dto.setId(id);
		SysUser sysUser = accountRpcService.findUserInfoByType(dto);
		if(sysUser == null) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		
		//获取角色信息
		SysUserRoleDto roleDto = new SysUserRoleDto();
		roleDto.setUserId(id);
		List<SysUserRole> sysUserRoles = accountRpcService.selectSysUserRoles(roleDto);
		if(sysUserRoles == null || sysUserRoles.size() == 0) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		List<SysRole> sysRoles = new ArrayList<>();
		for (SysUserRole sysUserRole : sysUserRoles) {
			SysRoleDto sysRoleDto = new SysRoleDto();
			sysRoleDto.setId(sysUserRole.getRoleId());
			sysRoleDto.setCid(sysUser.getCid());
			SysRole sysRole = roleRpcService.selectSysRoleByType(sysRoleDto);
			sysRoles.add(sysRole);
		}
		
		//获取门店信息
		BaseStoreToUserDto baseStoreToUserDto = new BaseStoreToUserDto();
		baseStoreToUserDto.setUserId(id);
		baseStoreToUserDto.setCid(sysUser.getCid());
		List<BaseStoreToUser> baseStoreToUsers = accountRpcService.selectUserStores(baseStoreToUserDto);
		if(baseStoreToUsers == null || baseStoreToUsers.size() == 0) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		List<BaseStoreInfo> baseStoreInfos = new ArrayList<>();
		for (BaseStoreToUser baseStoreToUser : baseStoreToUsers) {
			BaseStoreInfoDto baseStoreInfoDto = new BaseStoreInfoDto();
			baseStoreInfoDto.setId(baseStoreToUser.getStoreId());
			baseStoreInfoDto.setCid(sysUser.getCid());
			BaseStoreInfo baseStoreInfo = baseStoreInfoRpcService.findEntityByType(baseStoreInfoDto);
			if(baseStoreInfo != null) {
				baseStoreInfos.add(baseStoreInfo);
			}
		}
		
		//获取部门信息
		CompanyDepartmentDto departmentDto = new CompanyDepartmentDto();
		departmentDto.setId(sysUser.getDepartmentId());
		departmentDto.setCid(sysUser.getCid());
		CompanyDepartment companyDepartment = departmentRpcService.getCompanyDepartment(departmentDto);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("sysUser", sysUser);
		jsonObject.put("sysRole", sysRoles);
		jsonObject.put("stores", baseStoreInfos);
		jsonObject.put("departmentName", companyDepartment.getName() != null ? companyDepartment.getName() : "");
		
		model.setData(jsonObject);
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}
	
	/**
	 * 
	 * @Description:  修改姓名 邮箱 
	 * 
	 * @author: ligang
	 * @date:   2018年5月18日 下午4:02:58    
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxUpdateNameOrEmail(SysUser entity) {
		BaseResultModel baseResultModel = new BaseResultModel();
		//验证请求参数
		if(entity == null || entity.getId() == null) {
			baseResultModel.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return baseResultModel;
		}
		//查询用户信息
		SysUserDto dto = new SysUserDto();
		dto.setId(entity.getId());
		SysUser sysUser = accountRpcService.findUserInfoByType(dto);
		if(sysUser == null) {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return baseResultModel;
		}
		//更新用户信息
		entity.setUpdateTime(new Date());
		Integer status = accountRpcService.updateSelective(entity);
		if(status == 0) {
			baseResultModel.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return baseResultModel;
		}
		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}

	/**
	 * 
	 * @Description: 修改密码
	 * 
	 * @author: ligang
	 * @date:   2018年5月20日 上午10:40:53    
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxUpdatePassword(SysUserDto sysUserDto) {
		
		BaseResultModel model = new BaseResultModel();
		
		//校验参数
		if(sysUserDto == null || sysUserDto.getId() == null) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		
		//查询用户信息
		SysUser sysUser = accountRpcService.findUserInfoByType(sysUserDto);
		if(sysUser == null) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		
		String oldPassword = MD5Util.encodeString(MD5Util.encodeString(sysUserDto.getOldPassword())+ConfigUtil.MD5_PWD_STR);
		String newPassword = MD5Util.encodeString(MD5Util.encodeString(sysUserDto.getPwd())+ConfigUtil.MD5_PWD_STR);
		
		//校验旧密码与原密码
		if(!oldPassword.equals(sysUser.getPwd())) {
			model.setStatus(ResultStatus.NEED_UPDATE_ERROR.getStatusCode());
			model.setMessage("旧密码不匹配");
			return model;
		}
		
		//校验新旧密码
		if(newPassword.equals(sysUser.getPwd())) {
			model.setStatus(ResultStatus.NEED_UPDATE_ERROR.getStatusCode());
			model.setMessage("新老密码一致");
			return model;
		}
		
		//修改密码
		sysUser.setPwd(newPassword);
		sysUser.setUpdateTime(new Date());
		Integer result = accountRpcService.updateSelective(sysUser);
		if(result == 0) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return model;
		}
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
		
	}

	/**
	 * 
	 * @Description: 修改用户手机号
	 * 
	 * @author: ligang
	 * @date:   2018年5月21日 下午4:42:58    
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxUpdateMobile(SysUserDto userDto) {
		BaseResultModel model = new BaseResultModel();
		//校验参数
		if(userDto == null || StringUtils.isBlank(userDto.getAccount())) {
			model.setStatus(ResultStatus.REQUEST_PARAM_ERROR.getStatusCode());
			model.setMessage(ResultStatus.REQUEST_PARAM_ERROR.getStatusMessage());
			return model;
		}
		SysUser user = accountRpcService.findUserInfoByType(userDto);
		if(user != null) {
			model.setStatus(ResultStatus.ENTITY_EXIT.getStatusCode());
			model.setMessage(ResultStatus.ENTITY_EXIT.getStatusMessage());
			return model;
		}
		user = new SysUser();
		user.setId(userDto.getId());
		user.setAccount(userDto.getAccount());
		user.setUpdateTime(new Date());
		Integer result = accountRpcService.updateSelective(user);
		if(result <= 0) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return model;
		}
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
//		model.setData(0);
//		model.setTotalCount(0);
		return model;
	}

}
